package fun.hijklmn.magic.admin.server.handler;

import fun.hijklmn.magic.common.constants.CharsetConstant;
import fun.hijklmn.magic.common.constants.ContentTypeConstant;
import fun.hijklmn.magic.common.enums.ResponseCodeEnum;
import fun.hijklmn.magic.common.utils.JsonUtil;
import fun.hijklmn.magic.common.utils.ResultVOUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding(CharsetConstant.UTF8);
        httpServletResponse.setContentType(ContentTypeConstant.APPLICATION_JSON);
        httpServletResponse.getWriter().println(JsonUtil.toJsonString(ResultVOUtil.parse(ResponseCodeEnum.SUCCESS, "已退出")));
        httpServletResponse.getWriter().flush();
    }

}
